package com.yun.controller;


import com.yun.pojo.Director;
import com.yun.pojo.Message;
import com.yun.pojo.Schedule;
import com.yun.pojo.User;
import com.yun.service.DirectorService;
import com.yun.service.MessageService;
import com.yun.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class MessageController {

    @Autowired
    MessageService messageService;
    @Autowired
    DirectorService directorService;
    @Autowired
    UserService userService;
    @Autowired
    JavaMailSenderImpl mailSender;

    @RequestMapping("/getMessage")
    public String watchList(Model model){
        List<Message> messages = messageService.queryMessage();
        model.addAttribute("messages",messages);
        return "director/message";
    }
    @RequestMapping("/toSend")
    public String updateList(Model model){
        List<User> user = userService.getUser();
        Director director = directorService.queryEmail();
        model.addAttribute("user",user);
        model.addAttribute("director",director);
        return "director/send";
    }

    @RequestMapping("/send")
    public String back(@RequestParam("subject") String subject,
                       @RequestParam("text")String text,
                       @RequestParam("send")String send,
                       @RequestParam("receive")String receive,
                       Message message,Model model){
        SimpleMailMessage message1 = new SimpleMailMessage();
        message1.setSubject(subject);
        message1.setText(text);
        message1.setFrom(send);
        message1.setTo(receive);
        mailSender.send(message1);
        messageService.addMessage(message);
        List<Message> messages = messageService.queryMessage();
        model.addAttribute("messages",messages);
        return "director/message";
    }

    @RequestMapping("/deleteMessage/{id}")
    public String deleteMessage(@PathVariable("id")int id,Model model){
        messageService.deleteMessageById(id);
        List<Message> messages = messageService.queryMessage();
        model.addAttribute("messages",messages);
        return "director/message";
    }


}
